<?php

namespace App\Http\Controllers\Home;

use App\Http\Requests\Comment\Store;
use App\Models\Article;
use App\Models\Category;
use App\Models\Chat;
use App\Models\Comment;
use App\Models\OauthUser;
use App\Models\Tag;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    //快速排序
    public static function kuai($arr){
        if(count($arr)<=1){
            return $arr;
        }
        $leftarr=[];
        $rightarr=[];
        $first=array($arr[0]);
        $len=count($arr);
        for($i=1;$i<$len;$i++){
            if($arr[$i]>$first[0]){
                $rightarr[]=$arr[$i];
            }else if($arr[$i]<$first[0]){
                $leftarr[]=$arr[$i];
            }else{
                $first[]=$arr[$i];
            }
        }
        return array_merge(self::kuai($leftarr),$first,self::kuai($rightarr));

    }
    public static function query($v,$arr,$start,$end){
        $len=count($arr);
        $mid=$start+floor(($end-$start)/2);
//        echo $mid;
        if($v==$arr[$mid]){
            return $mid;
        }else if($v>$arr[$mid]){
            return self::query($v,$arr,$mid+1,$end);
        }else if ($v<$arr[$mid]){
            return self::query($v,$arr,$start,$mid-1);
        }

        return -1;
    }

    public function index()
    {
//        $arr=[4,3,66,53,74,89,0,53,87,4];
//        $len=count($arr);
//        //冒泡
//        for($i=0;$i<$len-1;$i++){
//            for($j=0;$j<$len-$i-1;$j++){
//                if($arr[$j]>$arr[$j+1]){
//                    $temp=$arr[$j+1];
//                    $arr[$j+1]=$arr[$j];
//                    $arr[$j]=$temp;
//                }
//            }
//        }
//        echo implode("-",$arr);
//        echo self::query(100,$arr,0,$len-1);
////        echo implode("-",self::kuai($arr));
//        exit;
        //
        $articles=Article::select(
            'id', 'category_id', 'title',
            'author', 'description', 'cover',
            'is_top', 'created_at'
        )
            ->orderBy('created_at', 'desc')
//            ->with(['category', 'tags'])
            ->paginate(10);
//        dd($articles);
        return view("home.index.index",compact('articles'));
    }

    /**
     * 获取文章详情
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function article($id,Comment $comment){
        $article=Article::with("tag","category")->findOrFail($id);
        $comments=$comment->getCommentByArticleId($id);
        //获取上下篇文章
        $preArticle=Article::select("id","title")
                            ->where("id","<",$article->id)
                            ->orderBy("id","desc")
                            ->limit(1)
                            ->first();
        $nextArticle=Article::select("id","title")
            ->where("id",">",$article->id)
            ->orderBy("id","asc")
            ->limit(1)
            ->first();
//        var_dump($comments);
        return view("home/index/article",compact('article',"comments","preArticle","nextArticle"));
    }

    /**
     * 发表评论
     * @param Store $store
     * @param Comment $comment
     * @return \Illuminate\Http\JsonResponse
     */
    public function comment(Store $store,Comment $comment){
        $data=$store->except("_token");
        $data['oauth_user_id']=session("user.id");
        $data['status']=1;
        $data['type']=1;
//        var_dump($data);exit;
        //添加评论
        $result=$comment->storeData($data);
//        mydump($result->toArray());
        if($result->pid){
            //查找被回复人用户名
            $reply_id=Comment::where('id',$result->pid)->value('oauth_user_id');
            //查找被回复的用户名
            $reply_name=OauthUser::where("id",$reply_id)->value("name");
//            mydump($reply_name);
            $result->reply_name=$reply_name;
        }
        return response()->json($result,200);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    /**
     * 获取标签下的文章
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function tag($id){
        $tag=Tag::select("id","name")->findOrFail($id);
        $articles=$tag->article()->paginate(10);
//        var_dump($articles);
        return view("home/index.index",compact("articles","tag"));
    }

    /**
     * 获取分类下的文章
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function category($id){
        $category=Category::select("id","name")->findOrFail($id);
        $articles=$category->article()->paginate(2);
//        var_dump($articles);
        return view("home/index.index",compact("articles","category"));
    }

    /**
     * 时间轴
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function time(){
        $chats=Chat::orderBy("created_at","desc")->get();
        return view("home/index/time",compact("chats"));
    }

    /**
     * 查询登录状态接口
     * @return int
     */
    public function islogin(){
        return session()->has("user")?1:0;
    }

    public function search(Request $request,Article $article){
        $keywords=$request->input("keywords");
        $articles=$article->searchArticle($keywords);
        return view("home.index.index",compact('articles','keywords'));
//        var_dump($articles);
    }
}
